package controllers;

import dto.Language;
import models.*;
import models.test.Contact;
import play.Logger;
import play.mvc.Controller;
import play.mvc.With;
import play.test.Fixtures;
import utils.ImageUtil;
import utils.SienaFixutre;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@With(Secure.class)
public class Test extends Controller{
    public static void selfRef() {
        List<Contact> contacts = Contact.all().fetch();
        render(contacts);
    }

    public static void addSelfRef() {
        Long parentId = params.get("parent", Long.class);
        Long childId = params.get("child", Long.class);

        Contact parent = Contact.findById(parentId);
        Contact child = Contact.findById(childId);
        
        parent.relatedsContact.asList().add(child);

        parent.save();

        redirect("Test.selfRef");
    }

    public static void addNew() {
        Contact c = new Contact();
        c.name = "name_" + Math.random();
        c.save();
        redirect("Test.selfRef");
    }

    public static void  changeName() {
        Long contactId = params.get("contactId", Long.class);
        Contact contact = Contact.findById(contactId);

        contact.name = params.get("name");

        contact.save();
        redirect("Test.selfRef");
    }

    public static void initData() {
        System.out.println("Will load data from data.yml");
        Fixtures.loadModels("data.yml");

        List<Topic> fetch = Topic.all().fetch();

        int i = 0;
        for (Topic one:fetch) {
            one.largePhoto = ImageUtil.getOneLargeImage();
            one.relatedPhoto = ImageUtil.getOneRelatedImage();
            i++;
            for (int j=0; j<i*2; j++) {
                one.moreInfos.asList().add((MoreInfo) MoreInfo.all().fetch().get(j));
            }

            for (int j=0; j<i; j++) {
                RelatedTopic relatedTopic = new RelatedTopic();
                relatedTopic.hostTopic = one.id;
                relatedTopic.relatedTopic = fetch.get(j).id;
                relatedTopic.save();
            }
            one.save();
        }

        Long numTopic = Topic.count();
        Long numMoreInfo = MoreInfo.count();

        render(numTopic, numMoreInfo);
    }
    public static void initTopic() {
        Logger.debug("begin=" + Topic.all().fetch().size());
        Topic.all().delete();
        try {
            //SienaFixutre.load("topic.yml");
            List<App> all = App.all(App.class).fetch();
            int n = 30;
            for (int i=0; i < n; i ++) {
                initOneTestTopic(all.get(0).code, i);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        addLargeImage();
        redirect("/test/index");
    }

    private static void initOneTestTopic(String appCode, int stt) {
        Topic t = new Topic();
        t.code = "code_" + stt;
        t.language = "vn";
        t.relatedPhoto = ImageUtil.toByte("cng.png", "../data/app/");
        t.appCode = appCode;
        t.desc = "<h2>mo tả cho sản phẩm thứ " + stt;
        
        t.largePhoto = ImageUtil.toByte("dt.png", "../data/app/");
        t.name = "Tên của sp thứ " + stt;
        t.title = "Tựa đề cho sp thứ " + stt;

        t.moreInfos.asList().addAll(getSampleMoreInfos());
        
        t.cost = Math.random()*100;
        t.brief = "Thong tin ngan gon mota ve San Pham thu " + stt;

        if (stt<13) {
            t.appCode = "cnt";
        } else {
            t.appCode = "pc";
        }
        t.save();
    }

    private static List<MoreInfo> getSampleMoreInfos() {
        int n = 5;
        List<MoreInfo> mores = new ArrayList<MoreInfo>();
        for (int i=0; i < n ; i ++) {
            MoreInfo more = new MoreInfo();
            more.link = "http://google.com";
            more.title = "Thong tin them " + i;

            mores.add(more);
        }
        
        return mores;
    }

    private static void addLargeImage() {
        Map<String, String> largeImageMap = new HashMap<String, String> ();
        List<Topic> topics = Topic.all().fetch();
        for (Topic t : topics) {
            String k = t.code + "_" + t.language;
            if (largeImageMap.containsKey(k)) {
                t.largePhoto = ImageUtil.toByte(largeImageMap.get(k));
                t.save();
            }
        }
    }


    public static void initMenu() {
        Logger.debug("Purge all data of Menu");
        Logger.debug("Number of deleted=" + Menu.all(Menu.class).delete());

        Logger.debug("Will load data from Menu.yml");
        try {
            SienaFixutre.load("data/menu.yml");
        } catch (Exception e) {
            e.printStackTrace();
        }
        Logger.debug("Number of topic after load menu.yml=" + Menu.all(Menu.class).count());

        //System.out.println(Menu.all(Menu.class));
        /*
        List<Menu> fetch = Menu.all(Menu.class).fetch();
        for(Menu m : fetch) {
            System.out.println(m.language + "|" + m.menuName + "|" + m.applicationCode);
        }
        */

        redirect("/test/index");
    }
    
    public static void index() {
        render();
    }

    public static void initNews() {
        TinTuc.all(TinTuc.class).delete();

        try {
            SienaFixutre.load("data/news.yml");
        } catch (Exception e) {
            e.printStackTrace();
        }

        redirect("/test/index");
    }

    public static void initTopicRelation() {
        List<Topic> allEn = Topic.all().filter(Language.LANGUAGE_KEY, Language.VN_CODE).fetch();
        for (int i=0; i < allEn.size(); i ++) {
            for (int j=i+1; j < allEn.size() ; j++) {
                RelatedTopic rt = new RelatedTopic();
                rt.relatedTopic = allEn.get(j).id;
                rt.hostTopic = allEn.get(i).id;
                
                rt.save();
            }
        }
        redirect("/test/index");
    }

    public static void initTopicSmallPhoto() {
        List<Topic> allEn = Topic.all().filter(Language.LANGUAGE_KEY, Language.EN_CODE).fetch();
        for (int i=0; i < allEn.size(); i ++) {
            allEn.get(i).relatedPhoto = ImageUtil.getOneRelatedImage();

            allEn.get(i).save();
        }
        redirect("/test/index");
    }


    public static void initApps() {
        App.all(App.class).delete();

        try {
            SienaFixutre.load("data/app.yml");
        } catch (Exception e) {
            e.printStackTrace();
        }

        addAppImage();

        redirect("/test/index");
    }

    private static void addAppImage() {
        List<App> topics = App.all(App.class).fetch();
        for (App t : topics) {
            String k = t.code;
            t.groupCode ="lvnn";
            t.photo = ImageUtil.toByte(k+".png", "../data/app/");
            t.save();
        }
    }

    public static void initBrands() {
        Brand.all(Brand.class).delete();

        try {
            SienaFixutre.load("data/brand.yml");
        } catch (Exception e) {
            e.printStackTrace();
        }

        addBrandImage();

        redirect("/test/index");
    }

    private static void addBrandImage() {
        Map<String, String> largeImageMap = new HashMap<String, String> ();
        largeImageMap.put("http://www.escoglobal.com", "4_hirayama.png");
        largeImageMap.put("http://www.ohaus.com", "ESCO.jpg");
        largeImageMap.put("http://www.velp.com", "evermed.jpg");
        largeImageMap.put("http://www.skalar.com", "oldham.jpg");

        List<Brand> topics = Brand.all(Brand.class).fetch();
        for (Brand t : topics) {
            String k = t.homepage;
            if (largeImageMap.containsKey(k)) {
                t.logo = ImageUtil.toByte(largeImageMap.get(k), "../data/brand/");
                t.save();
            }
        }
    }

    public static void initAppGroup() {
        initOneAppGroup("lvnn", "Linh vuc nong nghiep", "dt.png");
        initOneAppGroup("lvcn", "Linh vuc Cong Nghiep nang qua", "km.png");
        initOneAppGroup("lvyt", "Linh vuc Y te", "oncn.png");
        initOneAppGroup("mt", "Linh vuc Moi truong", "sxl.png");

        redirect("/test/index");
    }

    private static void initOneAppGroup(String code, String title, String imageName) {
        AppGroup ag = new AppGroup();
        ag.code = code;
        ag.title = title;
        ag.brief = "Brief about " + title;
        ag.language = "vn";

        ag.photo = ImageUtil.toByte(imageName, "../data/app/");
        ag.save();
    }
    
    public static void initBanners() {
        Banner.all(Banner.class).delete();

        try {
            SienaFixutre.load("data/banner.yml");
        } catch (Exception e) {
            e.printStackTrace();
        }

        addBannerImage();

        redirect("/test/index");
    }
    
    private static void addBannerImage() {
        List<Banner> all = Banner.all(Banner.class).fetch();
        for (Banner t : all) {
            String k = t.code;
            
            t.photo = ImageUtil.toByte(k+".jpg", "../data/app/");
            t.save();
        }
    }
}
